﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace SKDB_Matcher
{
    class CAlgorithm_Dice : CAbstractAlgorithm
    {
        public CAlgorithm_Dice()
        {
            Weight = 0.4f;
            Caption = "Dice";
        }

        override public float[,] CalcMatches(DataSet _Source, DataSet _Destination)
        {
            float[,] tmp = new float[SourceColumCount, TargetColumCount];
            String sA;
            String sB;
            String[] N_A;
            String[] N_B;
            int n = 0;
            int m = 0;
            foreach (DataTable TableSource in _Source.Tables)
            {
                foreach (DataColumn ColumnSource in TableSource.Columns)
                {
                    m = 0;
                    foreach (DataTable TableDest in _Destination.Tables)
                    {
                        foreach (DataColumn ColumnDest in TableDest.Columns)
                        {
                            sA = ColumnSource.ColumnName.ToLower();
                            sB = ColumnDest.ColumnName.ToLower();
                            sA = sA.Insert(0, "$$");
                            sA = sA.Insert(sA.Length, "$$");
                            sB = sB.Insert(0, "$$");
                            sB = sB.Insert(sB.Length, "$$");
                            N_A = new String[sA.Length - 2];
                            N_B = new String[sB.Length - 2];
                            for (int i = 0; i < N_A.Length; i++)
                                N_A[i] = sA.Substring(i, 3);
                            for (int i = 0; i < N_B.Length; i++)
                                N_B[i] = sB.Substring(i, 3);
                            int Matches = 0;
                            foreach (String SubA in N_A)
                                foreach (String SubB in N_B)
                                    if (String.Compare(SubA, SubB) == 0)
                                        Matches++;
                            float dice = ((2.0f * Matches) / (N_A.Length + N_B.Length));

                            tmp[n, m] = dice;
                            m++;
                        }
                    }
                    n++;
                }                
            }

            return tmp;
        }
    }


}
